package com.project.market.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.project.market.db.domain.MarketKeyword;
import com.project.market.service.AdminKeywordService;
import com.project.market.service.impl.AdminKeywordServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.format.DateTimeFormatter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/keyword/*")
public class AdminKeywordController extends HttpServlet {

    AdminKeywordService adminKeywordService = new AdminKeywordServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if ("create".equals(op)) {
            create(req, resp);
        } else if ("delete".equals(op)) {
            delete(req, resp);
        } else if ("update".equals(op)) {
            update(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String requestBody = req.getReader().readLine();
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        String url = JacksonUtil.parseString(requestBody, "url");

        MarketKeyword marketKeyword = adminKeywordService.update(addTime, id, keyword, updateTime, url);

        Object ok = ResponseUtil.ok(marketKeyword);
        String responseBody = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(responseBody);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");

        MarketKeyword marketKeyword = adminKeywordService.delete(addTime, id, keyword, updateTime);

        if (marketKeyword == null) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail()));
        }
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();
        String isDefault = JacksonUtil.parseString(requestBody, "isDefault");
        String isHot = JacksonUtil.parseString(requestBody, "isHot");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String url = JacksonUtil.parseString(requestBody, "url");

        Map<String, Object> map = adminKeywordService.create(isDefault, isHot, keyword, url);

        if (map == null) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail()));
        } else {
            String responseBody = JacksonUtil.writeValueAsString(ResponseUtil.ok(map));
            resp.getWriter().println(responseBody);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 获取前端传递过来的请求行的参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String keyword = req.getParameter("keyword");
        String url = req.getParameter("url");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        // 调用方法获得data
        List<MarketKeyword> list = adminKeywordService.list(page, limit, keyword, url, sort, order);

        // 封装到响应体中
        Object ok = ResponseUtil.okList(list);
        String responseBody = JacksonUtil.writeValueAsString(ok);
        resp.getWriter().println(responseBody);
    }
}
